package com.qdlc.p2p.biz.service.user.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.user.UserMobilePhoneService;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dao.UserIdentifyDao;
import com.qdlc.p2p.dal.dao.UserLoginExtDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.dto.UserLoginExt;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 用户手机号码
 * @author linya
 */
@Service
public class UserMobilePhoneServiceImpl implements UserMobilePhoneService{

	@Resource
	UserDao userDao;
	
	@Resource
	UserLoginExtDao userLoginExtDao;
	
	@Resource
	UserIdentifyDao userIdentifyDao;
	
	/**
	 * [绑定手机号码]检测绑定前条件
	 */
	public ReturnResult checkBindMobilePhone(User user, String code, String mobilePhone) {
		
		if (StringUtil.isBlank(mobilePhone)) {
			new ReturnResult("手机号码不能为空!");
		}
		
		if (!StringUtil.isPhone(mobilePhone)) {
			throw new UserException("手机号码格式不正确！", 1);
		}
		
		//检测手机号码是否被使用
		boolean exsitMobilePhone = UserUtils.exsitUserNameOrPhomeOrEmail(mobilePhone);
		if (exsitMobilePhone) {
			new ReturnResult("该手机号码已被使用!");
		}

		if (StringUtil.isBlank(code)) {
			new ReturnResult("验证码为空!");
		}
		
		//检测验证码
		ReturnResult result = ValidCodeSender.checkValidCode(user, code, NoticeTplNid.NOTICE_BIND_PHONE);
		
		return result;
	}
	
	
	/**
	 * 绑定用户手机号码
	 * @param user
	 * @param code
	 * @param mobilePhone
	 * @return
	 */
	@Override
	public ReturnResult bindMobilePhone(User user, String code, String mobilePhone){
		
		//检测手机号、验证码
		ReturnResult result = checkBindMobilePhone(user, code, mobilePhone);
		if(result.isError()){
			return result;
		}
		
		//修改用户手机号码
		modifyRegistMobilePhone(user, mobilePhone);
		
		return ReturnResult.SUCCESS;
	}
	
	/**
	 * 注册手机号码: 
	 * 1. 如果用户已经注册过手机，则只需修改UserLoginExt
	 * 2. 如果用户首次注册手机，则还需要设置手机的认证状态UserIdentify
	 * @param user
	 * @param mobilePhone
	 * @return
	 */
	@Override
	public void modifyRegistMobilePhone(User user, String mobilePhone) {
		
		//修改UserLoginExt，查询是否已经存在，如果存在则修改，不存在则插入
		UserLoginExt userExt = null;
		String oldMobilePhone = user.getMobilePhone();
		if(StringUtils.isNotBlank(oldMobilePhone)){
			userExt = userLoginExtDao.getUserByUserName(oldMobilePhone);
		}
		if(userExt == null){
			userExt = new UserLoginExt();
			userExt.setUserId(user.getUserId());
		}
		userExt.setUserName(mobilePhone);
		userLoginExtDao.merge(userExt);
		
		//修改用户认证UserIdentify
		UserIdentify userIdentify = UserUtils.getUserIdentify(user.getUserId());
		userIdentify.setMobilePhoneStatus(1);
		userIdentify.setMobilePhoneVerifyTime(DateUtil.getNow());
		userIdentifyDao.update(userIdentify);
		
		//修改User
		user.setMobilePhone(mobilePhone);
		userDao.update(user);
	}

}
